#include <iostream>
#include <random>
#include <vector>
#include <chrono>
#include "MemoryPool.h"

using namespace std;
#define MyAllocator MemoryPool
const int TestSize = 10000;
const int PickSize = 1000;

int main() {
    random_device rd;
    mt19937 gen(rd());
    uniform_int_distribution<> dis(1, TestSize);

    // 使用自定义分配器
    using IntVec = vector<int, MyAllocator<int>>;
    vector<IntVec, MyAllocator<IntVec>> vecints(TestSize);
    for (int i = 0; i < TestSize; i++)
        vecints[i].resize(dis(gen));

    using PointVec = vector<pair<int, int>, MyAllocator<pair<int, int>>>;
    vector<PointVec, MyAllocator<PointVec>> vecpts(TestSize);
    for (int i = 0; i < TestSize; i++)
        vecpts[i].resize(dis(gen));

    auto start_custom = chrono::high_resolution_clock::now();

    for (int i = 0; i < PickSize; i++) {
        int idx = dis(gen) - 1;
        int size = dis(gen);
        vecints[idx].resize(size);
        vecpts[idx].resize(size);
    }

    {
        int val = 10;
        int idx1 = dis(gen) - 1;
        int idx2 = vecints[idx1].size() / 2;
        vecints[idx1][idx2] = val;
        if (vecints[idx1][idx2] == val)
            cout << "correct assignment in vecints: " << idx1 << endl;
        else
            cout << "incorrect assignment in vecints: " << idx1;
    }
    {
        pair<int, int> val(11, 15);
        int idx1 = dis(gen) - 1;
        int idx2 = vecpts[idx1].size() / 2;
        vecpts[idx1][idx2] = val;
        if (vecpts[idx1][idx2] == val)
            cout << "correct assignment in vecpts: " << idx1 << endl;
        else
            cout << "incorrect assignment in vecpts: " << idx1;
    }

    auto end_custom = chrono::high_resolution_clock::now();
    chrono::duration<double> duration_custom = end_custom - start_custom;
    cout << "Custom allocator time: " << duration_custom.count() << " seconds" << endl;

    // 使用标准分配器
    using StdIntVec = vector<int>;
    vector<StdIntVec> std_vecints(TestSize);
    for (int i = 0; i < TestSize; i++)
        std_vecints[i].resize(dis(gen));

    using StdPointVec = vector<pair<int, int>>;
    vector<StdPointVec> std_vecpts(TestSize);
    for (int i = 0; i < TestSize; i++)
        std_vecpts[i].resize(dis(gen));

    auto start_std = chrono::high_resolution_clock::now();

    for (int i = 0; i < PickSize; i++) {
        int idx = dis(gen) - 1;
        int size = dis(gen);
        std_vecints[idx].resize(size);
        std_vecpts[idx].resize(size);
    }

    {
        int val = 10;
        int idx1 = dis(gen) - 1;
        int idx2 = std_vecints[idx1].size() / 2;
        std_vecints[idx1][idx2] = val;
        if (std_vecints[idx1][idx2] == val)
            cout << "correct assignment in std_vecints: " << idx1 << endl;
        else
            cout << "incorrect assignment in std_vecints: " << idx1;
    }
    {
        pair<int, int> val(11, 15);
        int idx1 = dis(gen) - 1;
        int idx2 = std_vecpts[idx1].size() / 2;
        std_vecpts[idx1][idx2] = val;
        if (std_vecpts[idx1][idx2] == val)
            cout << "correct assignment in std_vecpts: " << idx1 << endl;
        else
            cout << "incorrect assignment in std_vecpts: " << idx1;
    }

    auto end_std = chrono::high_resolution_clock::now();
    chrono::duration<double> duration_std = end_std - start_std;
    cout << "Standard allocator time: " << duration_std.count() << " seconds" << endl;

    return 0;
}
